// 
// Copyright 2010 Classpath Pty Ltd <opensource@classpath.com.au>
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// 

using build
using sql

**
** Tests the `MigrationMain` class.
**
internal class MigrationMainTest : Test {

    **
    ** Stores an instance of `MigrationMain` for testing.
    **
    MigrationMain? migrationMain

    **
    ** Performs additional setup needed for `MigrationMainTest`.
    **
    override Void setup() {
        super.setup()
        migrationMain = MigrationMainTestClass()
    }

    **
    ** Cleans up additional setup needed for `MigrationMainTest`.
    **
    override Void teardown() {
        super.teardown()
        migrationMain = null
    }

    **
    ** Tests `MigrationMain.checkUsage` with no arguments.
    **
    Void testCheckUsageNoArgs() {
        verifyFalse(migrationMain.checkUsage)
    }

    **
    ** Tests `MigrationMain.checkUsage` with -prepare specified.
    **
    Void testCheckUsageWithInit() {
        migrationMain.uri = "something"
        migrationMain.prepare = true
        verify(migrationMain.checkUsage)
    }

    **
    ** Tests `MigrationMain.checkUsage` with -prepare and -from specified.
    **
    Void testCheckUsageWithInitAndFrom() {
        migrationMain.uri = "something"
        migrationMain.prepare = true
        migrationMain.from = Version.defVal
        verifyFalse(migrationMain.checkUsage)
    }

    **
    ** Tests `MigrationMain.checkUsage` with -prepare and -to specified.
    **
    Void testCheckUsageWithInitAndTo() {
        migrationMain.uri = "something"
        migrationMain.prepare = true
        migrationMain.to = Version.defVal
        verifyFalse(migrationMain.checkUsage)
    }

    **
    ** Tests `MigrationMain.checkUsage` with -from and -to specified.
    **
    Void testCheckUsageWithInitAndFromAndTo() {
        migrationMain.uri = "something"
        migrationMain.prepare = true
        migrationMain.from = Version.defVal
        migrationMain.to = Version.defVal
        verify(migrationMain.checkUsage)
    }

    **
    ** Tests `MigrationMain.checkUsage` with -from specified.
    **
    Void testCheckUsageWithFrom() {
        migrationMain.uri = "something"
        migrationMain.from = Version.defVal
        verifyFalse(migrationMain.checkUsage)
    }

    **
    ** Tests `MigrationMain.checkUsage` with -to specified.
    **
    Void testCheckUsageWithTo() {
        migrationMain.uri = "something"
        migrationMain.to = Version.defVal
        verifyFalse(migrationMain.checkUsage)
    }

    **
    ** Tests `MigrationMain.checkUsage` with -from and -to specified.
    **
    Void testCheckUsageWithFromAndTo() {
        migrationMain.uri = "something"
        migrationMain.from = Version.defVal
        migrationMain.to = Version.defVal
        verify(migrationMain.checkUsage)
    }

    **
    ** Tests `MigrationMain.checkUsage` without -uri specified.
    **
    Void testCheckUsageWithoutUri() {
        migrationMain.prepare = true
        migrationMain.from = Version.defVal
        migrationMain.to = Version.defVal
        verifyFalse(migrationMain.checkUsage)
    }
}

**
** Provides a simple implementation of `MigrationMain` for testing.
**
internal class MigrationMainTestClass : MigrationMain {

    **
    ** Stores the list of [Migrations]`Migration` for testing.
    **
    override Migration[] migrations := [,]

    **
    ** Stores the `Adapter` for testing.
    **
    override Adapter adapter := MigrationMainTestAdapter()

}

**
** Provides a simple implementation of `Adapter` for testing.
**
internal class MigrationMainTestAdapter : Adapter {

    **
    ** Provides a test implementation of `Adapter.createTableSql`.
    **
    override Str createTableSql(CreatedTable table) {
        return Str.defVal
    }

    **
    ** Provides a test implementation of `Adapter.alterTableSql`.
    **
    override Str alterTableSql(AlteredTable table) {
        return Str.defVal
    }

    **
    ** Provides a test implementation of `Adapter.dropTableSql`.
    **
    override Str dropTableSql(DroppedTable table) {
        return Str.defVal
    }

    **
    ** Provides a test implementation of `Adapter.executeSql`.
    **
    override Str executeSql(ExecutedSql sql) {
        return Str.defVal
    }

    **
    ** Provides a test implementation of `Adapter.logMigrationSql`.
    **
    override Str logMigrationSql() {
        return Str.defVal
    }

    **
    ** Provides a test implementation of `Adapter.dbVersionSql`.
    **
    override Str dbVersionSql() {
        return Str.defVal
    }

}
